﻿using MicroMvvm;
using ModernUILogViewer.Business;
using ModernUILogViewer.Model;
using System;
using System.ComponentModel;
using System.Net;
using System.Reactive.Linq;
using System.Windows.Input;

namespace ModernUILogViewer.ViewModel
{
    public class NetworkViewModel : BaseViewModel, IDataErrorInfo
    {
        #region Constructor

        public NetworkViewModel()
        {
            this.updater = new ServiceUpdater();
        }
        
        #endregion

        #region EventHandler

        //public event EventHandler SettingsSaved;
        //public event EventHandler SettingsReloaded;

        public event EventHandler SoftwareOutOfDateEvent;
        public event EventHandler SoftwareUpToDateEvent;
        public event EventHandler NetworkErrorEvent;

        #endregion

        #region Fields

        private ServiceUpdater updater;

        #endregion

        #region Properties

        public string AppUri { get; set; }

        private bool enableProxy;

        public bool EnableProxy
        {
            get { return enableProxy; }
            set
            {
                enableProxy = value;
                RaisePropertyChanged(() => EnableProxy);
            }
        }

        private string host;

        public string Host
        {
            get { return host; }
            set
            {
                host = value;
                RaisePropertyChanged(() => EnableProxy);
            }
        }

        private int port;

        public int Port
        {
            get { return port; }
            set
            {
                port = value;
                RaisePropertyChanged(() => Port);
            }
        }

        private string user;

        public string User
        {
            get { return user; }
            set
            {
                user = value;
                RaisePropertyChanged(() => User);
            }
        }


        private string password;

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                RaisePropertyChanged(() => Password);
            }
        }

        private string domain;

        public string Domain
        {
            get { return domain; }
            set
            {
                domain = value;
                RaisePropertyChanged(() => Domain);
            }
        }

        #endregion

        #region Methods

        //void SaveNetworkSettingsExecuted()
        //{
        //    MessengerProvider.Instance.NotifyColleagues(MessengerEvents.MSG_NETWORK_SETTINGS_SAVED, this);

        //    RaiseEventInvoker(SettingsSaved);
        //}

        //void ReloadNetworkSettingsExecuted()
        //{
        //    MessengerProvider.Instance.NotifyColleagues(MessengerEvents.MSG_NETWORK_SETTINGS_RELOADED, this);

        //    RaiseEventInvoker(SettingsSaved);
        //}

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                if (this.GetType().GetProperty(columnName).Name.Equals(columnName))
                {
                }

                return null;
            }
        }

        void CheckAppUpdatesExecute()
        {
            //if (ModernUIHelper.IsInDesignMode == false)
            //{
            WebProxy proxy = null;

            if (enableProxy)
            {
                proxy = new WebProxy(host, port);
                proxy.Credentials = new NetworkCredential(user, password, domain);
            }

            ServiceUpdater updater = new ServiceUpdater(proxy);

            Observable.Start(() =>
            {
                ModernUILogViewer.Business.ServiceUpdater.VersionInfo version = null;

                try
                {
                    version = updater.CheckForUpdates(AppUri);
                }
                catch (Exception)
                {

                }

                return version;
            }).ObserveOnDispatcher().Subscribe<ModernUILogViewer.Business.ServiceUpdater.VersionInfo>(lastVersionInfo =>
            {
                if (lastVersionInfo != null)
                {
                    Version productVersion = Utility.GetProductVersion();

                    bool isVersionUpToDate = lastVersionInfo.LatestVersion <= productVersion;

                    RaiseEventInvoker(isVersionUpToDate ? SoftwareUpToDateEvent : SoftwareOutOfDateEvent, new DefaultEventArgs { ObjArg = lastVersionInfo });                    
                }
                else
                {
                    RaiseEventInvoker(NetworkErrorEvent);
                }
            });

        }

        #endregion

        #region Commands

        //public ICommand ReloadNetworkSettings { get { return new RelayCommand(ReloadNetworkSettingsExecuted); } }
        //public ICommand SaveNetworkSettings { get { return new RelayCommand(SaveNetworkSettingsExecuted); } }

        public ICommand CheckAppUpdates { get { return new MvvmFoundation.Wpf.RelayCommand(CheckAppUpdatesExecute); } }

        #endregion
    }
}
